﻿#region Copyright 
/*-------------------------------------------------------------------------
* 命名空间名称/文件名:    SSPivot.AspNetCore.Repository.DbContext/DbContextFactory 
* CLR版本:     4.0.30319.42000
* 机器名称:    DESKTOP-NHMLO8A
* 功 能：       N/A
* 类 名：       DbContextFactory
* 创建时间：  2025/5/26 10:44:52
* 版本号：      v1.0
* 创建人:        xulong
*-------------------------------------------------------------------------*/
#endregion
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using SSPivot.AspNetCore.Config;
using SSPivot.Common;
using SSPivot.Common.Helpers;
using SSPivot.Data;
using SSPivot.Data.Config;
using SSPivot.Data.MultiTenancy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SSPivot.AspNetCore.Repository
{
    public interface IDbContextFactory
    {
        DbContext CreateReadDbContext(string[] args);
        DbContext CreateDbContext(string[] args);
    }
    public abstract class DbContextFactory :IDbContextFactory
    {

        public abstract DbContext CreateReadDbContext(string[] args);
        //{
        //    var _configuration = BuildConfiguration();
        //    var config = _configuration.GetSection("DbConfig").Get<DbConfig>();// ConfigHelper.GetOptions<DbConfig>("DbConfig");
        //    var _loadBalancer = AppDependencyResolver.Current.GetService<IDbLoadBalancer>();
        //    var connectionString = _loadBalancer.GetReadConnection(config);
        //    var build = new DbContextOptionsBuilder<DbContext>();
        //    build.UseDatabase(config.DatabaseType, connectionString) ; 
        //    return new  AppDbContext<DbContext>(build.Options);
        //}
        public abstract DbContext CreateDbContext(string[] args);
        //{
        //    var _configuration = BuildConfiguration();
        //    var config = _configuration.GetSection("DbConfig").Get<DbConfig>(); //ConfigHelper.GetOptions<DbConfig>("DbConfig");
        //    var connectionString = config.MasterDb;
        //    var build = new DbContextOptionsBuilder<DbContext>();
        //    build.UseDatabase(config.DatabaseType, connectionString);
        //    return new AppDbContext<DbContext>(build.Options);
        //}
        private static IConfigurationRoot BuildConfiguration()
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(Path.Combine(Directory.GetCurrentDirectory()))
                .AddJsonFile("appsettings.json", optional: false);
            return builder.Build();
        }
    }
}
